package com.meiyu.live.ui.main;

import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.meiyu.live.BaseFragment;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.comm.UserConfig;
import com.meiyu.live.net.param.BaseBodyParams;
import com.meiyu.live.net.response.MyhookupResponse;
import com.meiyu.live.popj.Hookup;
import com.meiyu.live.tv.anchor.StartPlayActivity;
import com.meiyu.live.widget.NetLoadingDailog;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class StartHookFragment extends BaseFragment implements View.OnClickListener {
    private ImageView iv_avator, iv_qq, iv_wechat, iv_wxpy, iv_qzone, iv_sina;
    private TextView tv_category, tv_type, tv_starttime, tv_endtime, tv_next, tv_introduce;
    private View choose_share;
    private NetLoadingDailog dialog_loading;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    Map<Integer, SHARE_MEDIA> share_mediaMap = new HashMap<Integer, SHARE_MEDIA>();

    public static StartHookFragment newInstance() {
        StartHookFragment fragment = new StartHookFragment();
        return fragment;
    }

    @Override
    public int getContextView() {
        return R.layout.fragment_starthook;
    }

    @Override
    protected void init() {
        dialog_loading = new NetLoadingDailog(getActivity());
        imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.icon_def_header)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.icon_def_header)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.icon_def_header)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .build();


        iv_avator = (ImageView) findViewById(R.id.avator);
        iv_qq = (ImageView) findViewById(R.id.iv_qq);
        iv_wechat = (ImageView) findViewById(R.id.iv_wechat);
        iv_wxpy = (ImageView) findViewById(R.id.iv_wxpy);
        iv_qzone = (ImageView) findViewById(R.id.iv_qzone);
        iv_sina = (ImageView) findViewById(R.id.iv_sina);
        iv_qq.setOnClickListener(this);
        iv_wechat.setOnClickListener(this);
        iv_wxpy.setOnClickListener(this);
        iv_qzone.setOnClickListener(this);
        iv_sina.setOnClickListener(this);

        iv_qq.setTag(R.id.tag_default, R.drawable.qq_default);
        iv_qq.setTag(R.id.tag_sel, R.drawable.qq_sel);
        iv_qq.setTag(R.id.tag_status, 0);
        iv_wechat.setTag(R.id.tag_default, R.drawable.wechat_default);
        iv_wechat.setTag(R.id.tag_sel, R.drawable.wechat_sel);
        iv_wechat.setTag(R.id.tag_status, 0);
        iv_wxpy.setTag(R.id.tag_default, R.drawable.wxpy);
        iv_wxpy.setTag(R.id.tag_sel, R.drawable.wxpy_sel);
        iv_wxpy.setTag(R.id.tag_status, 0);
        iv_qzone.setTag(R.id.tag_default, R.drawable.qzone_default);
        iv_qzone.setTag(R.id.tag_sel, R.drawable.qzone_sel);
        iv_qzone.setTag(R.id.tag_status, 0);
        iv_sina.setTag(R.id.tag_default, R.drawable.sina_default);
        iv_sina.setTag(R.id.tag_sel, R.drawable.sina_sel);
        iv_sina.setTag(R.id.tag_status, 0);

        tv_introduce = (TextView) findViewById(R.id.tv_introduce);
        tv_category = (TextView) findViewById(R.id.tv_category);
        tv_type = (TextView) findViewById(R.id.tv_type);
        tv_starttime = (TextView) findViewById(R.id.tv_starttime);
        tv_endtime = (TextView) findViewById(R.id.tv_endtime);
        tv_next = (TextView) findViewById(R.id.btn_next);
        tv_next.setOnClickListener(this);
        getData();
    }

    private void getData() {
        dialog_loading.loading();
        BaseBodyParams b = new BaseBodyParams();
        b.setUrl(Constant.VERSION + Constant.UrlParams.MYHOOPUP);
        request(b, MyhookupResponse.class, new Response.Listener<MyhookupResponse>() {
            @Override
            public void onResponse(MyhookupResponse response) {
                if (response.getBody() != null && response.getBody().size() > 0) {
                    final Hookup hookup = response.getBody().get(0);
                    imageLoader.displayImage(UserConfig.getInstance().getPersonalInfo().getAvatar(), iv_avator, options);
                    tv_introduce.setText(hookup.getDescription());
                    tv_category.setText(hookup.getAssort_name());
                    tv_type.setText(hookup.getChannel_status() == 0 ? "公开" : "私密");
                    SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
                    String startTimeText = format.format(hookup.getStart_at() * 1000);
                    String endTimeText = format.format(hookup.getEnd_at() * 1000);
                    tv_starttime.setText(startTimeText);
                    tv_endtime.setText(endTimeText);
                }
            }
        });

//        b.setUrl(Constant.VERSION + Constant.UrlParams.CHECK_PERMISSIONS);
//        request(b, BaseResponse.class, new Response.Listener<BaseResponse>() {
//            @Override
//            public void onResponse(BaseResponse onlyStatusResponse) {
//                dialog_loading.dismissDialog();
//                if (onlyStatusResponse == null || onlyStatusResponse.getStatusCode() != Constant.StatusCode_Success) {
//                    tv_next.setBackgroundResource(R.drawable.background_btn_hui);
//                    tv_next.setClickable(false);
//                }
//            }
//        });

    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        super.onErrorResponse(volleyError);
    }

    private void changeStyleForShare(View view) {
        if (choose_share != null && choose_share.getId() == view.getId() && (int) view.getTag(R.id.tag_status) == 1) {
            ((ImageView) view).setImageResource((int) view.getTag(R.id.tag_default));
            choose_share = null;
        } else {
            if (choose_share != null) {
                ((ImageView) choose_share).setImageResource((int) choose_share.getTag(R.id.tag_default));
                view.setTag(R.id.tag_status, 0);
            }
            ((ImageView) view).setImageResource((int) view.getTag(R.id.tag_sel));
            view.setTag(R.id.tag_status, 1);
            choose_share = view;
        }
    }

    UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(getActivity(), "分享成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(getActivity(), "分享失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
//            Toast.makeText(getActivity(), "分享取消", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                if (choose_share != null) {
                    share_mediaMap.get(v.getId());
                    UMImage image = new UMImage(getActivity(), BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.ic_launcher));
                    new ShareAction(getActivity())
                            .setPlatform(share_mediaMap.get(choose_share.getId()))
                            .setCallback(umShareListener)
                            .withText("魅鱼直播")
                            .withTargetUrl("http://fir.im/maitotv")
                            .withMedia(image)
                            .share();
                }
                StartPlayActivity.start(getActivity(), null);
                break;
            case R.id.iv_qq:
            case R.id.iv_wechat:
            case R.id.iv_wxpy:
            case R.id.iv_sina:
            case R.id.iv_qzone:
                changeStyleForShare(v);
                break;
        }
    }
}
